#include "stdafx.h"
#include "Scheduler.h"
#include "IOStation.h"


Scheduler::Scheduler(double timeUnit, map<string, Station*>* stations) : timeUnit(timeUnit), stations(stations)
{
	triggers = new priority_queue<pair<double, Station*>, vector<pair<double, Station*>>, greater<pair<double, Station*>>>;
}


Scheduler::~Scheduler(void)
{
}

void Scheduler::run() {
	init();
	pair<double, Station*> current;
	while(running && triggers->size() > 0) {
//		System::Threading::Thread::Sleep((triggers->begin()->first - time)*timeUnit);
		current = triggers->top();
		triggers->pop();
		time = current.first;

		current.second->timeTrigger(time, this);
	}
	((IOStation*)(*stations)["io"])->stats->showStats(time);
}

void Scheduler::init() {
	running = true;
	time = 0;
	triggers->push(pair<double,Station*>(1,(*stations)["io"]));
} 

void Scheduler::stop() {
	running = false;
} 

void Scheduler::addTrigger(double time ,Station* station) {
	//triggers->insert(pair<double, Station*>(time, station));
	triggers->push(pair<double, Station*>(time, station));
}